﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PGMHelper;
using WHelper = PGMHelper.WilsonHelper;
using System.IO;
using Spectrum;
using System.Windows.Forms.DataVisualization.Charting;

namespace SpectrumTestForm
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 加速度时程曲线
        /// </summary>
        private AccelSet Accels { set; get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            //初始化加速度激励时程
            this.Accels = new AccelSet();
            //设定CHART外观
            this.TestChart.SetApperance(StringAlignment.Near, true, true, true);
            this.SpectrumChart.SetApperance(StringAlignment.Near, true, true, false);
            //添加坐标轴信息
            this.TestChart.AddTitles("激励时程", "时间 (s)", "加\n速\n度");
            this.SpectrumChart.AddTitles("反应谱", "周期 (s)", "影\n响\n系\n数");
            //X向坐标取整数
            this.TestChart.SetAxisFormat(ChartAxisType.AxisX, AxisFormat.Normal, 0);
            this.SpectrumChart.SetAxisFormat(ChartAxisType.AxisX, AxisFormat.Normal, 0);
        }

        /// <summary>
        /// 测试按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestButton_Click(object sender, EventArgs e)
        {
            //初始化
            string filePath = string.Empty;
            //获得文件路径
            if (!WHelper.OpenFile("文本文档(*.txt)|*.txt|所有文件(*.*)|*.*", ref filePath))
                return;
            //构造时程
            this.Accels = new AccelSet(filePath, 13, 1, 0.01F);
            //绘制加速度激励时程曲线
            this.DrawAccelList();
            //绘制反应谱曲线
            this.DrawSpectrum(true);
        }

        /// <summary>
        /// 绘制加速度激励时程
        /// </summary>
        private void DrawAccelList()
        {
            //清空序列
            this.TestChart.Series.Clear();
            //原激励时程
            this.TestChart.AddSeries("原激励", SeriesChartType.Line, Color.Red, 2);
            this.TestChart.Series["原激励"].Points.DataBindXY(
                this.Accels.GetOriginValueList("Time"),
                 this.Accels.GetOriginValueList("Accel"));
            //获得激励时程函数
            var accelNew = this.Accels.GetSpraseAccelHist(0.02F,30);
            //绘图
            this.TestChart.AddSeries("稀疏激励", SeriesChartType.Line, Color.Black, 2);
            this.TestChart.Series["稀疏激励"].Points.DataBindXY(
                accelNew.GetPropertyList("Time").ConvertAll(f => (float)f),
                accelNew.GetPropertyList("Accel").ConvertAll(f => (float)f));
        }

        /// <summary>
        /// 绘制加速度激励时程
        /// </summary>
        private void DrawSpectrum(bool isPeseudo = true)
        {
            //构造求解器
            var spectAlog = new SpectrumMethodParams(this.Accels, 0.05F, 6F, 0.01F, 0.02F);
            //求解反应谱
            var spect = spectAlog.GetSpectParam(true, 50);
            //清空Newmark
            spectAlog.ClearNewMark();
            //求解反应谱
            var spectE = spectAlog.GetSpectParam(true, 50);
            //绘制反应谱
            spect.ToChart(this.SpectrumChart, true);
            spectE.ToChart(this.SpectrumChart, false);
        }

        /// <summary>
        /// 地震动时间间隔不均匀 处理为均匀
        /// </summary>
        private void ExampleForEnjoy()
        {
            //初始化
            string filePath = string.Empty;
            //获得文件路径
            if (!WHelper.OpenFile("文本文档(*.txt)|*.txt|所有文件(*.*)|*.*", ref filePath))
                return;
            //构造加速度 跳过8行 用","拆分
            this.Accels = new AccelSet(filePath, 8, ",");
            //获得均匀的地震动 0.02s间隔
            var accelNew = this.Accels.GetSpraseAccelHist(0.02F, -1);
        }

        private void AccelGroupBox_Enter(object sender, EventArgs e)
        {

        }
    }
}
